package fh.app4stud.mensa;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;

/**
 * Created by IntelliJ IDEA.
 * User: ivan plese
 * Date: 09.04.11
 * Time: 15:17
 * Version 1.0
 * To change this template use File | Settings | File Templates.
 */
public class LocationListPreferences extends Activity {

    //static preferences
    public static final String PREF_AUTO_UPDATE = "PREF_AUTO_UPDATE";
    public static final String PREF_MAX_DIST = "PREF_MAX_DIST";

    //Views used in preferences
    CheckBox autoUpdateCheckBox;
    Spinner maxDistanceSpinner;

    SharedPreferences prefs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location_list_preferences);

        autoUpdateCheckBox = (CheckBox) findViewById(R.id.location_list_check_box_auto_update);
        maxDistanceSpinner = (Spinner) findViewById(R.id.location_list_spinner_distance);

        populateSpinners();

        /***
         * BUTTONS
         */
        Button okButton = (Button) findViewById(R.id.okButton);
        okButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //TODO save preferences
                savePreferences();
                LocationListPreferences.this.setResult(RESULT_OK);
                finish();
            }
        });
        Button cancelButton = (Button) findViewById(R.id.cancelButton);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                LocationListPreferences.this.setResult(RESULT_CANCELED);
                finish();
            }
        });

        Context context = getApplicationContext();
        prefs = PreferenceManager.getDefaultSharedPreferences(context);


        UpdateUIPreferences();

    }

    private void populateSpinners() {
        ArrayAdapter<CharSequence> distanceAdapter;
        distanceAdapter = ArrayAdapter.createFromResource(this, R.array.update_distance_options, android.R.layout.simple_spinner_item);

        int spinner_dd_item = android.R.layout.simple_spinner_dropdown_item;
        distanceAdapter.setDropDownViewResource(spinner_dd_item);
        maxDistanceSpinner.setAdapter(distanceAdapter);
    }

    private void UpdateUIPreferences() {
        boolean autoUpdateChecked = prefs.getBoolean(PREF_AUTO_UPDATE, false);
        int distanceIndex = prefs.getInt(PREF_MAX_DIST, 2);

        autoUpdateCheckBox.setChecked(autoUpdateChecked);
        maxDistanceSpinner.setSelection(distanceIndex);
    }

    private void savePreferences() {
        int updateIndex = maxDistanceSpinner.getSelectedItemPosition();
        boolean checked = autoUpdateCheckBox.isChecked();

        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(PREF_AUTO_UPDATE, checked);
        editor.putInt(PREF_MAX_DIST, updateIndex);
        editor.commit();
    }


}